package xyz.icxl.kotlion.ext.binding

import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


/**
 * RecyclerView 快速绑定一个适配器
 */
fun <T, Bind : ViewDataBinding> RecyclerView.bindQuickAdapter(
    @LayoutRes layoutId: Int,
    data: MutableList<T> = arrayListOf<T>(),
    onBind: (QA<T, Bind>) -> Unit,
): BaseQuickAdapter<T, BaseViewHolder> {
    val mAdapter = object : BaseQuickAdapter<T, BaseViewHolder>(layoutId, data) {
        override fun convert(holder: BaseViewHolder, item: T) {
            onBind(QA(item, DataBindingUtil.bind(holder.itemView),holder))
        }
    }
    adapter = mAdapter
    return mAdapter
}

/**
 * 创建一个适配器
 */
fun <T, Bind : ViewDataBinding> createQuickAdapter(
    @LayoutRes layoutId: Int,
    onBind: (QA<T, Bind>) -> Unit,
) = object : BaseQuickAdapter<T, BaseViewHolder>(layoutId) {
    override fun convert(holder: BaseViewHolder, item: T) {
        onBind(QA(item, DataBindingUtil.bind(holder.itemView), holder))
    }
}

class QA<T, Bind : ViewDataBinding>(
    val data: T,
    val bind: Bind?,
    val holder: BaseViewHolder
)